package jnpf.engine.custom.type;

import static jnpf.engine.custom.constants.GlobalProcessVariableConstants.PROCESS_STATUS;

import jnpf.engine.enums.FlowTaskStatusEnum;
import jnpf.engine.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.event.impl.FlowableProcessCancelledEventImpl;
import org.springframework.stereotype.Component;

//import org.flowable.common.engine.api.delegate.event.FlowableEvent;
//import org.flowable.common.engine.api.delegate.event.FlowableEventListener;

/**
 * @author LoveMyOrange
 * @create 2022-01-18 17:23
 */
@Slf4j
@Component
public class ProcessCancelListener implements FlowableEventListener {
    @Override
    public void onEvent(FlowableEvent event) {
        if (event instanceof FlowableProcessCancelledEventImpl) {
            FlowableProcessCancelledEventImpl activitiEntityEvent = (FlowableProcessCancelledEventImpl) event;
            DelegateExecution execution = activitiEntityEvent.getExecution();
            RuntimeService runtimeService = SpringContextHolder.getBean(RuntimeService.class);
            runtimeService.setVariable(execution.getProcessInstanceId(), PROCESS_STATUS,
                FlowTaskStatusEnum.Cancel.getCode());
        }
    }

    @Override
    public boolean isFailOnException() {
        return true;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }




}
